﻿using _5._6._2015CSharpDay08FullCRUDSingleton.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _5._6._2015CSharpDay08FullCRUDSingleton.Controllers
{
    public class HomeController : Controller
    {
        DB db = DB.Instance;
        public ActionResult Index()
        {
            return View(db.Movies);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }
        public ActionResult AddMovie()
        {
            ViewBag.Message = "Create A New Movie";
            return View("CreateEditMovie", new Movie());
        }

        [HttpPost]
        public ActionResult AddMovie(Movie model)
        {
            model.Id = db.Movies.LastOrDefault().Id + 1;
            db.Movies.Add(model);
            return RedirectToAction("Index");
        }
        public ActionResult EditMovie(int Id)
        {
            ViewBag.Message = "Edit This Movie";
            return View("CreateEditMovie", db.Movies.FirstOrDefault(x => x.Id == Id));
        }
        [HttpPost]
        public ActionResult EditMovie(int Id, string Title, string MovieUrl, MovieGenre Genre) 
        {
            var JarJarStinks = db.Movies.FirstOrDefault(x => x.Id == Id);
            JarJarStinks.Title = Title;
            JarJarStinks.MovieUrl = MovieUrl;
            JarJarStinks.Genre = Genre;
            return RedirectToAction("Index");
        }
        public ActionResult DeleteMovie(int Id)
        {
            Movie removeMovie = db.Movies.FirstOrDefault(x => x.Id == Id);
            db.Movies.Remove(removeMovie);
            return RedirectToAction("Index");
        }
    }
}